local rock2_assets =
{
    Asset("ANIM", "anim/sw_deeper_shadowstone.zip"),
}

local prefabs =
{
    "sw_shadow_seed",
    "dreadstone",
    "rock_break_fx",
}

SetSharedLootTable('sw_deeper_shadowstone',
    {
        { 'sw_shadow_seed', 1.00 },
        { 'sw_shadow_seed', 0.25 },
        { 'dreadstone',     1.00 },
        { 'dreadstone',     1.00 },
    })

local function OnWork(inst, worker, workleft)
    if workleft <= 0 then
        local pt = inst:GetPosition()
        SpawnPrefab("rock_break_fx").Transform:SetPosition(pt.x, pt.y, pt.z)
        inst.components.lootdropper:DropLoot(pt)

        inst:Remove()
    end
end

local function baserock_fn(bank, build, anim, minimapicon)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    MakeObstaclePhysics(inst, 0.25)

    if minimapicon then
        inst.MiniMapEntity:SetIcon(minimapicon)
    end

    inst.AnimState:SetBank(bank)
    inst.AnimState:SetBuild(build)

    if type(anim) == "table" then
        for i, v in ipairs(anim) do
            if i == 1 then
                inst.AnimState:PlayAnimation(v)
                inst.scrapbook_anim = v
            else
                inst.AnimState:PushAnimation(v, false)
            end
        end
    else
        inst.AnimState:PlayAnimation(anim)
        inst.scrapbook_anim = anim
    end

    inst:AddTag("boulder")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("lootdropper")

    local workable = inst:AddComponent("workable")
    workable:SetWorkAction(ACTIONS.MINE)
    workable:SetWorkLeft(TUNING.ROCKS_MINE)
    workable:SetOnWorkCallback(OnWork)

    inst:AddComponent("inspectable")

    MakeHauntableWork(inst)

    return inst
end

local function sw_deeper_shadowstone_fn()
    local inst = baserock_fn("sw_deeper_shadowstone", "sw_deeper_shadowstone", "idle", "sw_deeper_shadowstone.tex")

    if not TheWorld.ismastersim then
        return inst
    end

    inst.components.lootdropper:SetChanceLootTable('sw_deeper_shadowstone')

    return inst
end

return Prefab("sw_deeper_shadowstone", sw_deeper_shadowstone_fn, rock2_assets, prefabs)
